package com.dhcc.logs.common.queue.blockingqueue;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dhcc.logs.entity.SysOperLog;
import com.dhcc.logs.service.LogService;

/**
 * 
* <p>标题: LogSaveQueue.java</p>
* <p>业务描述:日志消费队列</p>
* <p>公司:东华软件股份公司</p>
* <p>版权:dhcc2013</p>
* @author 胡凯
* @date 2019年9月6日
* @version V1.0
 */
@Component
public class ConsumeLogQueue {
	private static final Logger logger = LoggerFactory.getLogger(ConsumeLogQueue.class);
//	@Reference(check = false)
	@Autowired
    private LogService logService;
	
	@PostConstruct
	public void startThread() {
		ExecutorService e = Executors.newFixedThreadPool(2);// 两个大小的固定线程池
		e.submit(new PollLog(logService));
		e.submit(new PollLog(logService));
	}

	class PollLog implements Runnable {
		LogService logService;

		public PollLog(LogService logService) {
			this.logService = logService;
		}
		@Override
		public void run() {
			while (true) {
				try {
					SysOperLog sysOperLog = LogSaveQueue.getQueue().consume();
					if (sysOperLog != null) {
						logger.info("剩余总数:{}",LogSaveQueue.getQueue().size());
						logService.save(sysOperLog);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	@PreDestroy
	public void stopThread() {
		logger.info("destroy");
	}
}
